package main;

import com.sun.jmx.snmp.Timestamp;
import org.msgpack.MessagePack;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TestSerialize {
	public static JdkSerializer jdkSer = new JdkSerializer();
	public static FstSerialize fstSer = new FstSerialize();
	public static MsgPackSerialize msgPackSer = new MsgPackSerialize();
	public static ProtostuffSerialize protoSer = new ProtostuffSerialize();
	public static HessianSerizlize hessian = new HessianSerizlize();
	public static KryoSerialize kryo = new KryoSerialize();
	
	
	public static void main(String[] args) throws IOException {
		Map<Integer, TestSerBean> list = new HashMap<Integer, TestSerBean>();
		TestSerBean  b=null;
		HeroManager heroManager = new HeroManager();
		for(int i =0;i<100;i++)
		{
			HeroModel hero = new HeroModel();
			hero.setHeroId(i);
			hero.setStart(i);
			hero.setName("文本");
			heroManager.addHeroModel(hero);
//			b = new TestSerBean();
//			b.id  = i;
//			b.a1  = i;
//			b.a2  = i;
//			b.a3  = i;
//			b.a4  = i;
//			list.put(b.getId(), b);
		}
		
		
		
		byte[] bs = fstSer.serialize(heroManager);
		System.out.println(bs.length);
		heroManager =  fstSer.unSerialize(bs, HeroManager.class);
		
		MessagePack msgpack = new MessagePack();
		// Serialize
		
		bs = msgpack.write(heroManager);
		System.out.println(bs.length);
		
		heroManager = msgpack.read(bs, HeroManager.class);
		
		bs= protoSer.serialize(heroManager);
		System.out.println(bs.length);
		heroManager = protoSer.unSerialize(bs, HeroManager.class);

		/* -------- protostuff -------- (requires protostuff-core module) */
		// ser
		
		Map<String,Object> object = new HashMap<String, Object>();
		String a = "a";
		int in = 1;
		long c = 2;
		BigInteger integer = BigInteger.TEN;//有问题
		long now =System.currentTimeMillis();
		Date date = new Date(now); //有问题
		System.out.println(now);
		Time time = new Time(now); //有问题
		Timestamp timestamp = new Timestamp(now); //有问题
		Double d= new Double(3.0);
		Float f = new Float(4.0);
		Boolean bool = new Boolean(true);
		BigDecimal  bigd = new BigDecimal(6);
		byte[] bytes = new byte[]{1,2};
		Map<String, Object> obj = new HashMap<String, Object>();
		
		obj.put("a", a);
		obj.put("b", b);
		obj.put("c", c);
//		obj.put("integer", integer);
//		obj.put("date", date);
//		obj.put("time", time);
//		obj.put("timestamp", timestamp);
		obj.put("d", d);
		obj.put("f", f);
		obj.put("bool", bool);
		obj.put("bigd", bigd);
		obj.put("bytes", bytes);
		
//		MessagePack msgpack = new MessagePack();
//		byte[] by = msgpack.write(obj);
//		
//		Value dynamic = msgpack.read(by);
//		obj = new Converter(dynamic)
//		    .read(Templates.tMap(Templates.TString, Templates.));
		for(int i =0;i<10000;i++)
		{
			byte[] by = fstSer.serialize(heroManager);
			heroManager = fstSer.unSerialize(by, HeroManager.class);
			//by = jdkSer.valueToBytes(heroManager);
			//heroManager = (HeroManager)jdkSer.valueFromBytes(by);
			by = msgPackSer.serialize(heroManager);
			heroManager = msgPackSer.unSerialize(by,HeroManager.class);
			
			by = protoSer.serialize(heroManager);
			protoSer.unSerialize(by,HeroManager.class);
			
			by = hessian.serialize(heroManager);
			heroManager = hessian.unSerialize(by,HeroManager.class);
			
			by = kryo.serialize(heroManager);
			kryo.unSerialize(by,HeroManager.class);

		}
	
		
		testLenth(heroManager);
		testTime(heroManager);
		
	}
	
	@SuppressWarnings("unchecked")
	public static void testLenth(HeroManager heroManager){
		
		
		byte[] by = fstSer.serialize(heroManager);
		System.out.println("fstLenth:"+by.length);
		
		
		by = jdkSer.serialize(heroManager);
		System.out.println("jdkLenth:"+by.length);
		

		by = msgPackSer.serialize(heroManager);
		System.out.println("msgPackSerLenth:"+by.length);
		
		by = protoSer.serialize(heroManager);
		System.out.println("protoSerLenth:"+by.length);
		
		
		by = hessian.serialize(heroManager);
		System.out.println("hessianSerLenth:"+by.length);
		
		by = kryo.serialize(heroManager);
		System.out.println("kryoSerLenth:"+by.length);
		
	}
	
	
	public static void testTime(HeroManager heroManager)
	{
		long start = System.currentTimeMillis();
		for(int i =0;i<100000;i++)
		{
			byte[] by = fstSer.serialize(heroManager);
			fstSer.unSerialize(by,HeroManager.class);
		}
		System.out.println("fst:"+(System.currentTimeMillis()-start));
		
		
		start = System.currentTimeMillis();
		for(int i =0;i<100000;i++)
		{
			byte[] by = jdkSer.serialize(heroManager);
			jdkSer.unSerialize(by,HeroManager.class);
		}
		System.out.println("jdk:"+(System.currentTimeMillis()-start));
		
		
		start = System.currentTimeMillis();
		for(int i =0;i<100000;i++)
		{
			byte[] by = msgPackSer.serialize(heroManager);
			msgPackSer.unSerialize(by,HeroManager.class);
		}
		System.out.println("msgpacker:"+(System.currentTimeMillis()-start));
		
		
		start = System.currentTimeMillis();
		for(int i =0;i<100000;i++)
		{
			byte[] by = protoSer.serialize(heroManager);
			protoSer.unSerialize(by,HeroManager.class);
		}
		System.out.println("protostuff:"+(System.currentTimeMillis()-start));
		
		
		start = System.currentTimeMillis();
		for(int i =0;i<100000;i++)
		{
			byte[] by = hessian.serialize(heroManager);
			hessian.unSerialize(by,HeroManager.class);
		}
		System.out.println("hessian:"+(System.currentTimeMillis()-start));
		
		
		start = System.currentTimeMillis();
		for(int i =0;i<100000;i++)
		{
			byte[] by = kryo.serialize(heroManager);
			kryo.unSerialize(by,HeroManager.class);
		}
		System.out.println("kryo:"+(System.currentTimeMillis()-start));
		
		
		
	}
	
	
}
